from django.db import models

from fbconnect.__init__ import *
from fbconnect.signals import facebook_update

from django.utils.translation import ugettext as _

# Try importing the django-profile BaseProfile
try:
    from userprofile.models import BaseProfile
except ImportError:

    class BaseProfile(models.Model):
        """
        User profile model
        """

        user = models.ForeignKey(User, unique = True)
        date = models.DateTimeField(default = datetime.datetime.now, blank = True, null = True)
        latitude = models.DecimalField(max_digits = 10, decimal_places = 6, blank = True, null = True)
        longitude = models.DecimalField(max_digits = 10, decimal_places = 6, blank = True, null = True)
        location = models.CharField(max_length = 255, blank = True, null = True)

        class Meta:
            abstract = True

        def __unicode__(self):
            return _("%s's profile") % self.user



class FBConnectProfile(BaseProfile):
    """ 
    User profile model with Facebook fields
    """
    gender_choices = (('male', _('Male')), ('female', _('Female')))
    fb_about_me = models.TextField(blank = True, null = True)
    fb_activities = models.TextField(blank = True, null = True)
    fb_birthday = models.DateField(blank = True, null = True)
    fb_current_location_city = models.CharField(max_length = 255, blank = True, null = True)
    fb_current_location_country = models.CharField(max_length = 255, blank = True, null = True)
    fb_current_location_state = models.CharField(max_length = 255, blank = True, null = True)
    fb_current_location_zip = models.CharField(max_length = 255, blank = True, null = True)

    fb_first_name = models.CharField(max_length = 255, blank = True, null = True)
    fb_last_name = models.CharField(max_length = 255, blank = True, null = True)

    fb_has_added_app = models.NullBooleanField(default = False, blank = True, null = True)
    fb_is_app_user = models.NullBooleanField(default = False, blank = True, null = True)
    fb_locale = models.CharField(max_length = 6, blank = True, null = True)

    fb_interests = models.TextField(blank = True, null = True)
    fb_movies = models.TextField(blank = True, null = True)
    fb_music = models.TextField(blank = True, null = True)

    fb_pic = models.URLField(blank = True, null = True, verify_exists = False)
    fb_pic_big = models.URLField(blank = True, null = True, verify_exists = False)
    fb_pic_small = models.URLField(blank = True, null = True, verify_exists = False)
    fb_pic_square = models.URLField(blank = True, null = True, verify_exists = False)

    fb_profile_update_time = models.DateTimeField(blank = True, null = True)


    fb_relationship_status = models.CharField(max_length = 255, blank = True, null = True)
    fb_religion = models.CharField(max_length = 255, blank = True, null = True)
    fb_sex = models.CharField(max_length = 7, blank = True, null = True, choices = gender_choices)
    fb_status_message = models.CharField(max_length = 255, blank = True, null = True)

    fb_timezone = models.CharField(max_length = 5, blank = True, null = True)

    fb_session_key = models.CharField(max_length = 255, blank = True, null = True)
    fb_uid = models.IntegerField(max_length = 255, blank = True, null = True)
    fb_secret = models.CharField(max_length = 255, blank = True, null = True)
    fb_sig = models.CharField(max_length = 255, blank = True, null = True)
    fb_expires = models.DateTimeField(blank = True, null = True)

    class Meta:
        abstract = True

    def update_fb_data(self, data, facebook, prefix = None):
        """ 
        New updating function taking the information from facebook.Facebook.users.getInfo
        """

        self._update_fb_data(data, prefix = prefix)

        facebook_update.send(sender = self, user = self.user, facebook = facebook)

    def _update_fb_data(self, data, prefix = None):
        for key in data:
            if type(data[key]) == dict:
                self._update_fb_data(data[key], prefix = key)
            if prefix:
                #print 'setting fb_%s_%s to %s' % ( prefix, key , data[key])
                self.__dict__['fb_%s_%s' % (prefix, key) ] = data[key]
            else:
                #print 'setting fb_%s to %s' % ( key , data[key])
                self.__dict__['fb_%s' % key] = data[key]

